import QtQuick 2.7
import QtQuick.Controls 1.4


TableView{
    id: tableview
    property var holdMenuModelDataArray: null
    backgroundVisible: false;
    frameVisible: false;
    itemDelegate: standardDelegate //Cell
    headerDelegate: headerDele;  //表头委托 
    rowDelegate: rowDele;   //行委托 

    //定义表头的委托
    Component{
        id:headerDele
        Rectangle{
            id: headerRect;
            height: 40;
            width: 80
            border.color: "white";
            color:(styleData.value.length>0)?"#c3fafb":"transparent"
            radius: 3;
            Text{
                text: styleData.value
                anchors.centerIn: parent
                font.family: "微软雅黑";
                font.pixelSize: 12;
                color: "black";
            }
        }
    }
    Component{
        id: standardDelegate
        Rectangle{
            id: tableCell
            anchors.fill: parent;
            anchors.margins: 3;
            border.color: "blue";
            radius:3;
            color: (styleData.column===0) ?  Qt.rgba(1,0.99,0.55,1): "transparent";

            height:42
            Text{
                id: textID;
                text:String(styleData.value) ;
                font.family: "微软雅黑";
                font.pixelSize: 12;
                //font.underline: (styleData.column===tableview.columnCount-1)
                anchors.fill: parent;
                color: "black";
                //elide: Text.ElideRight;
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }
        }
    }

    //定义行的委托
    Component{
        id:rowDele
        Rectangle{
            id: rect;
            height: 42;
            color: "transparent";
        }
    }
    //更新表格标题列高度和role
    function initColumns(column_config){
        var col_count=tableview.columnCount;
        for(var i=0;i<col_count;i++){
            tableview.removeColumn(0);
        }
        var prefix = "import QtQuick 2.7;import QtQuick.Controls 1.4;TableViewColumn {width: (tableview.width-20)/"; //创建TableViewColumn的代码

        //循环添加TableViewColumn
        var totalWidth=0;
        for(var i=0;i< column_config.length;i++){
//            print("i",i,arrayRoles[i],arrayTitle[i]);
            var str = prefix+ String(column_config.length) +";role:\"" + column_config[i]["role"] + "\";title:\"" +  column_config[i]["title"] + "\"}";
            var curCol=tableview.addColumn(Qt.createQmlObject(str,tableview,"dynamicSnippet1"));
            //totalWidth+=curCol.width;
        }
        //第一列列宽太窄时设置列宽为80
        var w=tableview.getColumn(0).width;
        tableview.getColumn(0).width=(w<80)?80:w;
        totalWidth=totalWidth-w+(w<80)?80:w;
    }

}
